package com.atguigu.spzx.cart.controller;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.common.Interceptor.SpzxUserInfoInterceptor;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.entity.user.UserInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syh
 * @create 2023-09-02 11:15
 */
@Tag(name = "购物车接口")
@RestController
@RequestMapping("api/order/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "删除用户购物车中选中的购物项")
    @DeleteMapping("/auth/deleteCheckedCartInfo")
    public Result deleteCheckedCartInfo(){
        cartService.deleteCheckedCartInfo();
        return Result.ok(null);
    }

    @Operation(summary = "添加购物车")
    @GetMapping("auth/addToCart/{skuId}/{count}")
    public Result addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                            @Parameter(name = "count", description = "数量") @PathVariable("count") Integer count) {
        cartService.addToCart(skuId, count);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary ="查询购物车")
    @GetMapping("/auth/cartList")
    public Result cartList(){
        List<CartInfo> cartInfoList = cartService.cartList();
        return Result.build(cartInfoList, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新购物项的选中状态")
    @GetMapping("/auth/checkCart/{skuId}/{status}")
    public Result checkCart(@PathVariable("skuId")Long skuId,
                            @PathVariable("status")Integer status){
        cartService.checkCart(skuId , status);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "全选")
    @GetMapping("/auth/allCheckCart/{status}")
    public Result allCheckCart(@PathVariable("status")Integer status){
        cartService.allCheckCart(status);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除单个购物项")
    @DeleteMapping("/auth/deleteCart/{skuId}")
    public Result deleteCartInfo(@PathVariable("skuId")Long skuId){
        cartService.deleteCartInfo(skuId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "清空购物车")
    @GetMapping("/auth/clearCart")
    public Result clearCart(){
        cartService.clearCart();
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "查询下单用户选中的购物项列表")
    @GetMapping("/auth/getAllCheckedCartInfo")
    public Result<List<CartInfo>> getAllCheckedCartInfo(){
        List<CartInfo> cartInfoList = cartService.getAllCheckedCartInfo();
        return Result.build(cartInfoList,ResultCodeEnum.SUCCESS);
    }

}
